﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Rotempco.Website.Logic;
using Rotempco.Core.Web.Logic;
using Rotempco.Core.Web.Objects.Enums;

namespace Rotempco.Website.MasterPages.UserControls
{
    public partial class EditBottomPanel : System.Web.UI.UserControl
    {
        public bool DisableReturnToListAfterSaveEvent { get; set; }
        public bool DisableCancelButton {  set { _ForceEnabled_.Visible = !value; } }
        public string Text { set { Button_Save.Text = value; } }
        public string ReturnUrl { get; set; }

        public delegate bool SaveDelegate();
        public event SaveDelegate SaveEvent;

        public delegate void CancelDelegate();
        public event CancelDelegate CancelEvent;

        protected void Page_Load(object sender, EventArgs e)
        {
            label_ErrorMessage.Text = "";
        }

        public void SetErrorMessage(string errorMessage)
        {
            label_ErrorMessage.Text = errorMessage;
        }

        /// <summary>
        /// As default it will return to the list view
        /// </summary>.
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void button_CancelClick(object sender, EventArgs e)
        {
            if (CancelEvent != null)
                CancelEvent();
            else
                ReturnToList();

        }

        /// <summary>
        /// As default it redirect to the list after the save event, this behavior can be disabled with the Option "DisableReturnToListAfterSaveEvent"
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="args"></param>
        protected void button_SaveClick(object obj, EventArgs args)
        {
            if (SaveEvent != null)
            {
                if (SaveEvent() && !DisableReturnToListAfterSaveEvent)
                    ReturnToList();
            }
        }

        private void ReturnToList()
        {
            Response.Redirect(ReturnUrl);
        }
    }
}